package com.youtills.core.app.pcc;

import java.util.Date;

/**
 * TODO - !!!doc me!!!
 *
 * @author ravi.chiluveru
 * @since 23.0 (9/12/14)
 */
public enum ParkingSpace {

    A1_6200("6200", "A1"),
    A2_6200("6200", "A2"),
    B1_6200("6200", "B1"),
    B2_6200("6200", "B2"),

    A1_6210("6210", "A1"),
    A2_6210("6210", "A2"),
    B1_6210("6210", "B1"),
    B2_6210("6210", "B2"),

    A1_6220("6220", "A1"),
    A2_6220("6220", "A2"),
    B1_6220("6220", "B1"),
    B2_6220("6220", "B2"),

    A1_6230("6230", "A1"),
    A2_6230("6230", "A2"),
    B1_6230("6230", "B1"),
    B2_6230("6230", "B2");


    public static final ParkingSpace[] S_6200 = {
        A1_6200,
        A2_6200,
        B1_6200,
        B2_6200
    };

    public static final ParkingSpace[] S_6210 = {
        A1_6210,
        A2_6210,
        B1_6210,
        B2_6210
    };

    public static final ParkingSpace[] S_6220 = {
        A1_6220,
        A2_6220,
        B1_6220,
        B2_6220
    };

    public static final ParkingSpace[] S_6230 = {
        A1_6230,
        A2_6230,
        B1_6230,
        B2_6230
    };


    private String parkingSpaceName;
    private String stationName;
    private Status status;
    private int userId;
    private String carName;
    private Date modifiedAt;

    ParkingSpace(String stationName, String parkingSpaceName) {
        this.parkingSpaceName = parkingSpaceName;
        this.stationName = stationName;
        this.status = Status.Available;
        updateNow();
    }

    public Status getStatus() {
        return status;
    }

    public Date getModifiedAt() {
        return modifiedAt;
    }

    public int getUserId() {
        return userId;
    }

    public String getCarName() {
        return carName;
    }

    public String getStationName() {
        return stationName;
    }

    public String getParkingSpaceName() {
        return parkingSpaceName;
    }

    public String getName(){
        return name();
    }

    public enum Status {
        Available,
        Waiting,
        Charging,
        Complete;

        public String getName() {
            return name();
        }
    }

    public boolean isCi(){
        return status == Status.Available;
    }

    public boolean isCo(){
        return status == Status.Waiting || status == Status.Charging || status == Status.Complete;
    }

    public boolean isPi(){
        return status == Status.Waiting || status == Status.Available;
    }

    public boolean isCc(){
        return status == Status.Waiting || status == Status.Charging;
    }


    private void updateNow(){
        this.modifiedAt = new Date();
    }

    public void doAction(EvUser user, PccEvRequestHandler.Action action){
        switch (action) {
            case ci:
                if(isCi() && user.getParkingSpace() == null) {
                    this.status = Status.Waiting;
                    this.userId = user.getId();
                    this.carName = user.getCarName();
                    user.setParkingSpace(this);
                    updateNow();
                }
                break;
            case co:
                if(isCo() && (this.userId == user.getId() || user.isAdmin())) {
                    this.status = Status.Available;
                    if(this.userId > 0) {
                        EvUser.evUsers[this.userId - 1].setParkingSpace(null);
                    }
                    this.userId = 0;
                    this.carName = "";
                    updateNow();
                }
                break;
            case pi:
                if(isPi()) {
                    if (this.status == Status.Available) {
                        this.userId = user.getId();
                        this.carName = user.getCarName();
                        user.setParkingSpace(this);
                    }
                    this.status = Status.Charging;
                    updateNow();
                }
                break;
            case cc:
                if(isCc()) {
                    this.status = Status.Complete;
                    updateNow();
                }
                break;

        }
    }

}
